package cn.fhkj.web.controller;

import cn.fhkj.bean.ResultBean;
import cn.fhkj.config.SevenNiuProperties;
import cn.fhkj.dao.RenqiuZfEmailMapper;
import cn.fhkj.dao.RenqiuZfPublicityReconsiderationMapper;
import cn.fhkj.entity.RenqiuZfEmail;
import cn.fhkj.entity.RenqiuZfPublicityReconsideration;
import cn.fhkj.util.DocUtil;
import cn.fhkj.util.SendMailUtil;
import cn.fhkj.util.SimpleUpload;
import cn.fhkj.vo.ReconsiderationVo;
import com.qiniu.common.QiniuException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: renqiu-publicity-web
 * @description:
 * @author: seven
 * @create: 2020-08-17 16:52
 **/
@RequestMapping("/")
@Controller
@CrossOrigin
public class RenQiuReconsiderationController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(RenQiuReconsiderationController.class);

    @Autowired
    private SevenNiuProperties fileProperties;
    @Resource
    private RenqiuZfPublicityReconsiderationMapper renqiuZfPublicityReconsiderationMapper;

    @Resource
    private RenqiuZfEmailMapper renqiuZfEmailMapper;

    private static final String mail_smtp = "smtp";
    private static final String mail_host = "smtp.pluslegal.cn";
    private static final String mail_sendName = "legalplus@pluslegal.cn";
    private static final String mail_sendPort = "25";
    //QQ端口
    //private static final String mail_sendPort = "465";
    private static final String mail_send_userName = "legalplus@pluslegal.cn";
    private static final String mail_send_userPwd = "Fahkj@#1234";
    private static final String mail_send_nick = "法和科技";

    private static final String FIXED_SENNDMAIL = "renqiushifazhiban@163.com";
   // private static final String FIXED_SENNDMAIL ="legalplus2017@163.com";

    @RequestMapping(value = "admin_list")
    public String admin_list(ReconsiderationVo entity) {
        return "admin_list";
    }


    @RequestMapping(value = "apply_step1")
    public String apply_step1(ReconsiderationVo entity) {
        return "apply_step1";
    }


    @RequestMapping(value = "apply_step2")
    public String apply_step2(ReconsiderationVo entity) {
        HttpSession session = super.request.getSession();
        //个人
        session.setAttribute("applicantName", entity.getApplicantName());
        session.setAttribute("applicantSex", entity.getApplicantSex());
        session.setAttribute("applicantBirthday", entity.getApplicantBirthday());
        session.setAttribute("applicantCerName", entity.getApplicantCerName());
        session.setAttribute("applicantCerNum", entity.getApplicantCerNum());
        session.setAttribute("applicantWorkUnit", entity.getApplicantWorkUnit());
        session.setAttribute("applicantLocation", entity.getApplicantLocation());
        session.setAttribute("applicantPostalCode", entity.getApplicantPostalCode());
        session.setAttribute("applicantPhone", entity.getApplicantPhone());
        session.setAttribute("applicantEmail", entity.getApplicantEmail());
        //法人
        session.setAttribute("applicantCompanyName", entity.getApplicantCompanyName());
        session.setAttribute("applicantCompanyNum", entity.getApplicantCompanyNum());
        session.setAttribute("applicantPerson", entity.getApplicantPerson());
        session.setAttribute("applicantLegalPerson", entity.getApplicantLegalPerson());
        session.setAttribute("applicantLegalPhone", entity.getApplicantLegalPhone());
        session.setAttribute("applicantLegalEmail", entity.getApplicantLegalEmail());
        session.setAttribute("applicantType", entity.getApplicantType());

        return "apply_step2";
    }

    @RequestMapping(value = "apply_step3")
    public String apply_step3(ReconsiderationVo entity) {
        HttpSession session = super.request.getSession();

        session.setAttribute("respondentName", entity.getRespondentName());
        session.setAttribute("respondentLegalName", entity.getRespondentLegalName());
        session.setAttribute("respondentLocation", entity.getRespondentLocation());
        session.setAttribute("respondentPhone", entity.getRespondentPhone());
        return "apply_step3";
    }

    @RequestMapping(value = "apply_step4")
    public String apply_step4(ReconsiderationVo entity) {
        HttpSession session = super.request.getSession();

        session.setAttribute("agentName", entity.getAgentName());
        session.setAttribute("agentNation", entity.getAgentNation());
        session.setAttribute("agentSex", entity.getAgentSex());
        session.setAttribute("agentVocation", entity.getAgentVocation());
        session.setAttribute("agentBirthday", entity.getAgentBirthday());
        session.setAttribute("agentPostalCode", entity.getAgentPostalCode());
        session.setAttribute("agentLocation", entity.getAgentLocation());
        session.setAttribute("agentPhone", entity.getAgentPhone());
        session.setAttribute("agentFixedphone", entity.getAgentFixedphone());
        session.setAttribute("agentAddress", entity.getAgentAddress());

        session.setAttribute("agentCompanyName", entity.getAgentCompanyName());
        session.setAttribute("agentCompanyNum", entity.getAgentCompanyNum());
        session.setAttribute("agentLegalPerson", entity.getAgentLegalPerson());
        session.setAttribute("agentContacts", entity.getAgentContacts());
        session.setAttribute("agentContactsPhone", entity.getAgentContactsPhone());
        session.setAttribute("agentContactsEmail", entity.getAgentContactsEmail());
        session.setAttribute("agentType", entity.getAgentType());

        return "apply_step4";
    }

    @RequestMapping(value = "apply_step5")
    public String apply_step5(ReconsiderationVo entity) {
        HttpSession session = super.request.getSession();
        session.setAttribute("reconsiderationRequest", entity.getReconsiderationRequest());
        return "apply_step5";
    }

    @RequestMapping(value = "apply_step6")
    public String apply_step6(ReconsiderationVo entity) throws QiniuException {
        HttpSession session = super.request.getSession();
        session.setAttribute("factsEvidence", entity.getFactsEvidence());

        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("applicantName", session.getAttribute("applicantName"));
        Integer applicantSex = (Integer) session.getAttribute("applicantSex");
        if (applicantSex != null) {
            if (applicantSex == 0) {
                dataMap.put("applicantNex", "男");
            } else {
                dataMap.put("applicantNex", "女");
            }
        }

        dataMap.put("applicantBirthday", session.getAttribute("applicantBirthday"));
        dataMap.put("applicantCerName", session.getAttribute("applicantCerName"));
        dataMap.put("applicantCerNum", session.getAttribute("applicantCerNum"));
        dataMap.put("applicantWorkUnit", session.getAttribute("applicantWorkUnit"));
        dataMap.put("applicantLocation", session.getAttribute("applicantLocation"));
        dataMap.put("applicantPostalCode", session.getAttribute("applicantPostalCode"));
        dataMap.put("applicantPhone", session.getAttribute("applicantPhone"));
        dataMap.put("applicantEmail", session.getAttribute("applicantEmail"));

        dataMap.put("applicantCompanyName", session.getAttribute("applicantCompanyName"));
        dataMap.put("applicantCompanyNum", session.getAttribute("applicantCompanyNum"));
        dataMap.put("applicantPerson", session.getAttribute("applicantPerson"));
        dataMap.put("applicantLegalPerson", session.getAttribute("applicantLegalPerson"));
        dataMap.put("applicantLegalPhone", session.getAttribute("applicantLegalPhone"));
        dataMap.put("applicantLegalEmail", session.getAttribute("applicantLegalEmail"));
        dataMap.put("applicantType", session.getAttribute("applicantType"));

        dataMap.put("respondentName", session.getAttribute("respondentName"));
        dataMap.put("respondentLegalName", session.getAttribute("respondentLegalName"));
        dataMap.put("respondentLocation", session.getAttribute("respondentLocation"));
        dataMap.put("respondentPhone", session.getAttribute("respondentPhone"));

        dataMap.put("agentName", session.getAttribute("agentName"));
        dataMap.put("agentNation", session.getAttribute("agentNation"));
        Integer agentSex = (Integer) session.getAttribute("agentSex");
        if (agentSex != null) {
            if (agentSex == 0) {
                dataMap.put("agentSex", "男");
            } else {
                dataMap.put("agentSex", "女");
            }
        }
        dataMap.put("agentVocation", session.getAttribute("agentVocation"));
        dataMap.put("agentBirthday", session.getAttribute("agentBirthday"));
        dataMap.put("agentPostalCode", session.getAttribute("agentPostalCode"));
        dataMap.put("agentLocation", session.getAttribute("agentLocation"));
        dataMap.put("agentPhone", session.getAttribute("agentPhone"));
        dataMap.put("agentFixedphone", session.getAttribute("agentFixedphone"));
        dataMap.put("agentAddress", session.getAttribute("agentAddress"));

        dataMap.put("agentCompanyName", session.getAttribute("agentCompanyName"));
        dataMap.put("agentCompanyNum", session.getAttribute("agentCompanyNum"));
        dataMap.put("agentLegalPerson", session.getAttribute("agentLegalPerson"));
        dataMap.put("agentContacts", session.getAttribute("agentContacts"));
        dataMap.put("agentContactsPhone", session.getAttribute("agentContactsPhone"));
        dataMap.put("agentContactsEmail", session.getAttribute("agentContactsEmail"));
        dataMap.put("agentType", session.getAttribute("agentType"));

        dataMap.put("reconsiderationRequest", session.getAttribute("reconsiderationRequest"));

        dataMap.put("factsEvidence", session.getAttribute("factsEvidence"));


        DocUtil docUtil = new DocUtil();
        long suffix = System.currentTimeMillis();
        docUtil.createDoc(dataMap, "text7", "D:\\行政复议申请书" + suffix + ".doc");
        // 上传到七牛云

        SimpleUpload simpleUplad = new SimpleUpload(fileProperties.getAccessKey(), fileProperties.getSecretKey(),
                fileProperties.getBucketName(), fileProperties.getPipeline());
        File file = new File("D:\\行政复议申请书" + suffix + ".doc");
        long length = file.length();
        String fileSize = Math.round(length * 0.1 / 1024 * 10) + "kb";
        String fileName = "行政复议申请书" + suffix + ".doc";
        session.setAttribute("fileSize", fileSize);
        session.setAttribute("fileName", fileName);
        String uploadFile = simpleUplad.uploadFile(file, "contract/" + suffix + ".doc");

        String laborSavePath = uploadFile.split(",")[1].split(":")[1].replaceAll("\"", "").replaceAll("}", "");

        session.setAttribute("laborSavePath", laborSavePath);
        RenqiuZfEmail email = new RenqiuZfEmail();
        email.setDocPath(laborSavePath);
        email.setDocName(fileName);
        email.setDocSize(fileSize);
        renqiuZfEmailMapper.insert(email);
        session.setAttribute("projectId", email.getId());
        return "apply_step6";
    }

    @RequestMapping(value = "addReconsideration")
    @ResponseBody
    public ResultBean addReconsideration() {
        ResultBean<String> resultBean = new ResultBean<String>();

        HttpSession session = super.request.getSession();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            RenqiuZfPublicityReconsideration entity = new RenqiuZfPublicityReconsideration();
            entity.setApplicantName((String) session.getAttribute("applicantName"));
            entity.setApplicantSex((Integer) session.getAttribute("applicantSex"));
            entity.setApplicantBirthday((String) session.getAttribute("applicantBirthday"));
            entity.setApplicantCerName((String) session.getAttribute("applicantCerName"));
            entity.setApplicantCerNum((String) session.getAttribute("applicantCerNum"));
            entity.setApplicantWorkUnit((String) session.getAttribute("applicantWorkUnit"));
            entity.setApplicantLocation((String) session.getAttribute("applicantLocation"));
            entity.setApplicantPostalCode((String) session.getAttribute("applicantPostalCode"));
            entity.setApplicantPhone((String) session.getAttribute("applicantPhone"));
            entity.setApplicantEmail((String) session.getAttribute("applicantEmail"));

            entity.setApplicantCompanyName((String) session.getAttribute("applicantCompanyName"));
            entity.setApplicantCompanyNum((String) session.getAttribute("applicantCompanyNum"));
            entity.setApplicantPerson((String) session.getAttribute("applicantPerson"));
            entity.setApplicantLegalPerson((String) session.getAttribute("applicantLegalPerson"));
            entity.setApplicantLegalPhone((String) session.getAttribute("applicantLegalPhone"));
            entity.setApplicantLegalEmail((String) session.getAttribute("applicantLegalEmail"));

            entity.setRespondentName((String) session.getAttribute("respondentName"));
            entity.setRespondentLegalName((String) session.getAttribute("respondentLegalName"));
            entity.setRespondentLocation((String) session.getAttribute("respondentLocation"));
            entity.setRespondentPhone((String) session.getAttribute("respondentPhone"));

            entity.setAgentName((String) session.getAttribute("agentName"));
            entity.setAgentNation((String) session.getAttribute("agentNation"));
            entity.setAgentSex((Integer) session.getAttribute("agentSex"));
            entity.setAgentVocation((String) session.getAttribute("agentVocation"));
            entity.setAgentBirthday((String) session.getAttribute("agentBirthday"));
            entity.setAgentPostalCode((String) session.getAttribute("agentPostalCode"));
            entity.setAgentLocation((String) session.getAttribute("agentLocation"));
            entity.setAgentPhone((String) session.getAttribute("agentPhone"));
            entity.setAgentFixedphone((String) session.getAttribute("agentFixedphone"));
            entity.setAgentAddress((String) session.getAttribute("agentAddress"));

            entity.setAgentCompanyName((String) session.getAttribute("agentCompanyName"));
            entity.setAgentCompanyNum((String) session.getAttribute("agentCompanyNum"));
            entity.setAgentLegalPerson((String) session.getAttribute("agentLegalPerson"));
            entity.setAgentContacts((String) session.getAttribute("agentContacts"));
            entity.setAgentContactsPhone((String) session.getAttribute("agentContactsPhone"));
            entity.setAgentContactsEmail((String) session.getAttribute("agentContactsEmail"));
            entity.setReconsiderationRequest((String) session.getAttribute("reconsiderationRequest"));
            entity.setFactsEvidence((String) session.getAttribute("factsEvidence"));
            entity.setType((Integer) session.getAttribute("type"));
            entity.setApplicantType((Integer) session.getAttribute("applicantType"));
            entity.setAgentType((Integer) session.getAttribute("agentType"));
            //默认未查看
            entity.setStatus(0);
            entity.setCreateTime(sdf.format(new Date()));
            int insert = renqiuZfPublicityReconsiderationMapper.insert(entity);
            //向指定邮箱发送邮件
            String projectIdStr = request.getParameter("projectId");
            Long projectId = Long.valueOf(projectIdStr);
            fixedSendMail(projectId);
            resultBean.setSuccess(ResultBean.SUCCESS);
            resultBean.setMsg("添加成功");
        } catch (Exception e) {
            logger.error("", e);
            resultBean.setMsg("添加失败");
            resultBean.setSuccess(ResultBean.FAIL);
        }
        return resultBean;
    }

    @ResponseBody
    public ResultBean fixedSendMail(Long projectId) {
        ResultBean resultBean = new ResultBean();
        logger.info("传进来的参数", projectId);
        try {
            String mail_subject = "";
            String mail_content = "";
            RenqiuZfEmail mail = renqiuZfEmailMapper.selectByPrimaryKey(projectId);
            logger.info("邮件对象", mail);
            String laborSavePath = mail.getDocPath();
            String fileName = mail.getDocName();
            String fileSize = mail.getDocSize();
            logger.info("邮件大小，名字", fileName, fileSize);
            mail_subject = "申请书：";
            String href = fileProperties.getFile_url_pre() + "/" + laborSavePath;
            logger.info("邮件路径", href);
            mail_content = "<div style='width:45%;text-align: center;margin:0 auto'>"
                    + "<div style='border-bottom:1px solid #F0F0F0;height:2rem;line-height:2rem;font-size:0.8rem;color:#526477;'>"
                    + "<img style='float:left;width:1.5rem;margin-left:0.5rem;' src='http://otxm0pud0.bkt.clouddn.com/image/icon_legal_e_mial@2x.png'>"
                    + "<span style='float:right'>法加，您的随行法律专家&nbsp;法加智慧法务助手</span>" + "</div>"

                    + "<h2 style='font-size:2rem;'>您好，有新的行政复议申请！</h2>"
                    + "<p style='color:#526477;font-size:0.8rem;'>请及时登录管理后台进行处理，后台地址 http://39.98.123.49:8091/</p>"

                    + "<div style='text-align:left;'>" + "<span style='font-size:1.1rem;font-weight:bold;'>文件名称：</span>"
                    + "<span style='font-size:0.9rem;color:#C6CBD2;'>" + fileName + "</span>" + "</div>"

                    + "<div style='text-align:left;margin:1rem 0 2rem 0;'>"
                    + "<span style='font-size:1.1rem;font-weight:bold;'>文件大小：</span>"
                    + "<span  style='font-size:0.9rem;color:#C6CBD2;'>" + fileSize + "</span>" + "</div>"

                    + "<a href='" + href
                    + "' style='text-decoration:none;display:block;width:65%;margin:0 auto;height:3.5rem;line-height:3.5rem;background:#1595E7;font-size:1.6rem;color:white;font-weight: 800;border-radius:3rem;-ms-border-radius: 3rem;-moz-border-radius: 3rem;-webkit-border-radius:3rem;'>下载文件</a>"

                    + "<div style='float:right;text-align:left;'>"
                     + "</div>" + "</div> " + "</div>";

            logger.info("发送邮件前————————————————————————————————");
            SendMailUtil.send(FIXED_SENNDMAIL, mail_subject, mail_content, mail_smtp, mail_host, mail_sendName,
                    mail_sendPort, mail_send_userName, mail_send_userPwd, mail_send_nick);
            logger.info("发送出去邮件！！！！！！！！！！！！！！！！！！！！！");
            resultBean.setMsg("发送成功");
            resultBean.setSuccess(ResultBean.SUCCESS);
        } catch (Exception e) {
            logger.error("", e);
            resultBean.setMsg("发送失败");
            resultBean.setSuccess(ResultBean.FAIL);
        }
        return resultBean;
    }


    @ResponseBody
    @RequestMapping(value = "sendMail")
    public ResultBean sendMail(String to_email, Long projectId) {
        ResultBean resultBean = new ResultBean();
        logger.info("传进来的参数", to_email, projectId);
        try {
            String mail_subject = "";
            String mail_content = "";
            RenqiuZfEmail mail = renqiuZfEmailMapper.selectByPrimaryKey(projectId);
            logger.info("邮件对象", mail);
            String laborSavePath = mail.getDocPath();
            String fileName = mail.getDocName();
            String fileSize = mail.getDocSize();
            logger.info("邮件大小，名字", fileName, fileSize);
            mail_subject = "申请书：";
            String href = fileProperties.getFile_url_pre() + "/" + laborSavePath;
            logger.info("邮件路径", href);
            mail_content = "<div style='width:45%;text-align: center;margin:0 auto'>"
                    + "<div style='border-bottom:1px solid #F0F0F0;height:2rem;line-height:2rem;font-size:0.8rem;color:#526477;'>"
                    + "<img style='float:left;width:1.5rem;margin-left:0.5rem;' src='http://otxm0pud0.bkt.clouddn.com/image/icon_legal_e_mial@2x.png'>"
                    + "<span style='float:right'>法加，您的随行法律专家&nbsp;法加智慧法务助手</span>" + "</div>"

                    + "<h2 style='font-size:2rem;'>亲爱的法加用户您好！</h2>"
                    + "<p style='color:#526477;font-size:0.8rem;'>您的行政复议申请书生成，请点击下方链接查看/下载详情</p>"

                    + "<div style='text-align:left;'>" + "<span style='font-size:1.1rem;font-weight:bold;'>文件名称：</span>"
                    + "<span style='font-size:0.9rem;color:#C6CBD2;'>" + fileName + "</span>" + "</div>"

                    + "<div style='text-align:left;margin:1rem 0 2rem 0;'>"
                    + "<span style='font-size:1.1rem;font-weight:bold;'>文件大小：</span>"
                    + "<span  style='font-size:0.9rem;color:#C6CBD2;'>" + fileSize + "</span>" + "</div>"

                    + "<a href='" + href
                    + "' style='text-decoration:none;display:block;width:65%;margin:0 auto;height:3.5rem;line-height:3.5rem;background:#1595E7;font-size:1.6rem;color:white;font-weight: 800;border-radius:3rem;-ms-border-radius: 3rem;-moz-border-radius: 3rem;-webkit-border-radius:3rem;'>下载文件</a>"
                    + "<p style='font-size:1.2rem;width:65%;margin:2rem auto 2rem auto;text-align: left;color:#C6CBD2;'>合同文本及具体条款，不是标准及最终法律文本，仅供参考，法加建议根据实际情况，在法律专业人士的指导下进行修改后再使用，法加就在线上生成合同不承担任何的法律责任。</p>"
                    + "<div style='float:left;text-align: left;width:50%;'>" + "<p>客服热线：010-8424-9028</p>"
                    + "<p>周一至周五 09:00-20:00</p>" + "</div> "

                    + "<div style='float:left;text-align: left;margin-bottom:2rem;'>" + "<p>客服邮箱：xiaof@pluslegal.cn</p>"
                    + "<p>合作邮箱：legalplus@pluslegal.cn</p>" + "</div>"

                    + "<div style='clear:both;width:50%;margin:0 auto'>" + " <div style='float:left;text-align:right;'>"
                    + "<img style='width:8.5rem;' src='http://otxm0pud0.bkt.clouddn.com/image/img_code_1@3x.png' alt='法加公众号'>"
                    + "<p style='text-align: center'>法加公众号</p>" + "</div>"

                    + "<div style='float:right;text-align:left;'>"
                    + " <img style='width:8.5rem;' src='http://otxm0pud0.bkt.clouddn.com/image/img_code_2@3x.png' alt='法加APP'>"
                    + "<p style='text-align: center'>法加APP</p> " + "</div>" + "</div> " + "</div>";
            logger.info("发送邮件前————————————————————————————————");
            SendMailUtil.send(to_email, mail_subject, mail_content, mail_smtp, mail_host, mail_sendName,
                    mail_sendPort, mail_send_userName, mail_send_userPwd, mail_send_nick);
            logger.info("发送出去邮件！！！！！！！！！！！！！！！！！！！！！");
            resultBean.setMsg("发送成功");
            resultBean.setSuccess(ResultBean.SUCCESS);
        } catch (Exception e) {
            logger.error("", e);
            resultBean.setMsg("发送失败");
            resultBean.setSuccess(ResultBean.FAIL);
        }
        return resultBean;
    }


}
